package com.otitan.forest.right.util;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Optional;

/**
 * @Description: 调用外部 exe 外部程序工具包
 * @Author: jiachangsen
 * @CreateDate: 2020/08/13 10:23
 * @UpdateUser: jiachangsen
 * @UpdateDate: 2020/08/13 10:23
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
public class CellOtherProgram {

    /**
     * 可执行程序路径
     */
    private String exePath;
    /**
     * 文件路径
     */
    private String filePath;
    /**
     * 类型
     *  SingleShp：单个图形上传
     *  MultiData：多数据上传
     */
    private String type;
    /**
     * 操作用户 ID
     */
    private String userId;
    /**
     * 数据关联 主键
     */
    private String appId;
    /**
     * 省
     */
    private String province;
    /**
     * 市
     */
    private String city;
    /**
     * 县
     */
    private String county;

    /**
     * 任务ID
     */
    private String guid;

    public CellOtherProgram(String exePath, String filePath, String userId, String appId, String province, String city, String county, String guid) {
        this.exePath = exePath;
        this.filePath = filePath;
        this.userId = userId;
        this.appId = appId;
        this.province = province;
        this.city = city;
        this.county = county;
        this.guid = guid;
    }

    /**
     * 调用 exe 程序
     * @author jiachangsen
     * @param []
     * @return void
     * @exception 
     * @date 2020/08/13 10:25
     */
    public void send(){
        StringBuffer cell = cell(exePath,
                "SingleShp",
                filePath,
                appId,
                guid
        );
        if (cell != null){
            log.debug("\n【程序调用】 - 类型：批量导入。任务ID：{}\n返回结果：{}", guid, cell.toString());
        }

    }


    public void batchSend(){

        StringBuffer cell = cell(exePath,
                "MultiData",
                filePath,
                userId,
                guid,
                Optional.ofNullable(province).orElse(""),
                Optional.ofNullable(city).orElse(""),
                Optional.ofNullable(county).orElse("")
                );
        if (cell != null){
            log.debug("\n【程序调用】 - 类型：批量导入。任务ID：{}\n返回结果：{}", guid, cell.toString());
        }
    }



    public StringBuffer cell(String ...string){
        Process process = null;
        StringBuffer sb = null;
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            log.debug("【调用exe，参数】：{}", Arrays.toString(string));
            process = new ProcessBuilder(string).start();
            is = process.getInputStream();
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
            String line;
            sb = new StringBuffer();
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null){
                    br.close();
                }
                if (isr != null){
                    isr.close();
                }
                if (is != null){
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb;
    }


}
